﻿using System;
using System.ComponentModel;
using System.Linq;

namespace ManageDto
{
    /// <summary>
    /// 性别
    /// </summary>
    public enum Gender : int
    {
        [Description("未知")]
        Unknown = 1,
        [Description("男")]
        Male = 2,
        [Description("女")]
        Female = 3
    }

    /// <summary>
    /// 公司分类(客观业务逻辑)
    /// </summary>
    [Flags]
    public enum CompanyClassification : int
    {
        [Description("集团总部")]
        Headquarter = 1,
        [Description("区域单位")]
        RegionalCompany = 2,
        [Description("城市公司")]
        CityCompany = 4,
        [Description("项目公司")]
        ProjectCompany = 8,
        [Description("事业单位")]
        BusinessUnit = 16,
        [Description("子公司")]
        Subcompany = 32
    }

    /// <summary>
    /// 用户关联类型
    /// </summary>
    public enum RelType : int
    {
        [Description("部门")]
        Dept = 1,
        [Description("岗位")]
        Post = 2,
        [Description("设备")]
        Device = 3
    }

    public sealed class SystemType
    {
        /// <summary>
        /// 获取指定枚举成员的描述
        /// </summary>
        public static string ToDescriptionString<T>(int value) where T : struct
        {
            var values = from Enum e in Enum.GetValues(typeof(T))
                         select new { id = Convert.ToInt32(e), name = e.ToString() };

            var dic = values.ToDictionary(a => a.id, b => b.name);
            T res = default(T);
            if (dic.ContainsKey(value))
                Enum.TryParse<T>(dic[value], true, out res);
            else
                return string.Empty;

            var attribs = (DescriptionAttribute[])res.GetType().GetField(res.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
            return attribs.Length > 0 ? attribs[0].Description : res.ToString();
        }

        /// <summary>
        /// int转时间
        /// </summary>
        public static DateTime GetDTByInt(int i)
        {
            Int64 begtime = Convert.ToInt64(i) * 10000000;
            DateTime dt_1970 = new DateTime(1970, 1, 1, 8, 0, 0);
            long tricks_1970 = dt_1970.Ticks;   //1970年1月1日刻度
            long time_tricks = tricks_1970 + begtime;   //日志日期刻度
            DateTime dt = new DateTime(time_tricks);    //转化为DateTime
            return dt.Date;
        }

        /// <summary>
        /// 时间转int
        /// </summary>
        public static int GetIntByDT(DateTime dt)
        {
            int result = 0;
            DateTime startdate = new DateTime(1970, 1, 1, 8, 0, 0);
            DateTime end = new DateTime(dt.Year, dt.Month, dt.Day); //只精确到天
            TimeSpan seconds = end - startdate;
            result = Convert.ToInt32(seconds.TotalSeconds);
            return result;
        }

        /// <summary>
        /// int转时间
        /// </summary>
        public static bool HasRecent(int i)
        {
            var dt = GetDTByInt(i);
            if (dt < DateTime.Now && dt.AddDays(5) > DateTime.Now)
                return true;
            else
                return false;
        }
    }
}
